﻿
using ICarlifeRespository;
using ICarlifeService;
using ICarlifeService.IKeHu;
using MathNet.Numerics.RootFinding;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Models;
using Models.Clientx;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarlifeService
{
    public class HuanKuan : BaseServer<Repayment>, IHuanKuan
    {
        private readonly IHuanKuanory db;
        private readonly IUOfWork uOfWork;
        private readonly IHKDTO hKDTO;
        private readonly IKeHuRepository huRepository;
        private readonly IClienttypeRepository clienttype;
        private readonly ISocialRelationships socialRelationships;

        public HuanKuan(IHuanKuanory db, IUOfWork uOfWork, IHKDTO hKDTO, IClienttypeRepository clienttype, IKeHuRepository huRepository, ISocialRelationships socialRelationships) : base(db, uOfWork)
        {
            this.db = db;
            this.uOfWork = uOfWork;
            this.hKDTO = hKDTO;
            this.clienttype = clienttype;
            this.huRepository = huRepository;
            this.socialRelationships = socialRelationships;
        }

        public List<RepaymentDTO> RepaymentShow(DateTime? StartTime, DateTime? EndTime, string? ClientId = "", string? ClientName = "", int ClientTypeId = 0, int BusinessManagementID = 0)
        {
#pragma warning disable CS8601 // 引用类型赋值可能为 null。
            var list = from x in db.GetAll()
                       join y in clienttype.GetAll()
                         on x.ClientTypeId equals y.Id
                       join a in huRepository.GetAll()
                      on x.ClientId equals a.Id
                       join b in clienttype.GetAll()
                     on x.BusinessManagementID equals b.Id
                       join c in clienttype.GetAll()
                         on x.IsoverdueID equals c.Id

                       select new RepaymentDTO
                       {
                           Id = a.Id,
                           ClientId = a.ClientId,
                           ClientName = a.ClientName,
                           TypeName = y.TypeName,
                           Count = x.Count,
                           Rent = x.Rent,
                           Surplusrent = x.Surplusrent,
                           RentalDay = x.RentalDay,
                           DelayedInterest = x.DelayedInterest,
                           BusinessManagementName = b.TypeName,
                           IsoverdueName = c.TypeName,
                           BusinessManagementID = x.BusinessManagementID,
                           ClientTypeId = x.ClientTypeId,
                           IsoverdueID = x.IsoverdueID,
                       };
#pragma warning restore CS8601 // 引用类型赋值可能为 null。
            //if (ClientId != null)
            //{
            //    list = list.Where(x => Convert.ToInt32(x.ClientId.Contains(ClientId)));
            //}
            if (ClientName != null)
            {
                list = list.Where(x => x.ClientName.Contains(ClientName));
            }
            if (ClientTypeId != 0)
            {
                list = list.Where(x => x.ClientTypeId == ClientTypeId);
            }
            if (BusinessManagementID != 0)
            {
                list = list.Where(a => a.BusinessManagementID == BusinessManagementID);
            }
            if (StartTime != null)
            {
                list = list.Where(x => x.RentalDay >= Convert.ToDateTime(StartTime));
            }
            if (EndTime != null)
            {
                list = list.Where(x => x.RentalDay <= (Convert.ToDateTime(EndTime)));
            }
            return list.ToList();
        }


        public List<ClientDTO> ClientBackfill(int id)
        {
            var list = from x in db.GetAll()
                       join y in huRepository.GetAll().Where(y => y.Id == id)
                       on x.ClientId equals y.Id
                       join a in clienttype.GetAll()
                    on y.ClientTypeId equals a.Id
                       join b in socialRelationships.GetAll()
                      on y.SocialID equals b.Id
                      join c in clienttype.GetAll()
                      on y.ClientTypeId equals c.Id

                       select new ClientDTO
                       {
                           ClientId = y.ClientId,
                           ClientName = y.ClientName,
                           Phone = y.Phone,
                           Id = y.Id,
                           TypeName = c.TypeName,
                           IdNumber = y.IdNumber,
                           ChannelSource = y.ChannelSource,
                           ClientTypeId = y.ClientTypeId,
                           Department = y.Department,
                           DrivingAge = y.DrivingAge,
                           LengthOfStay = y.LengthOfStay,
                           Marriage = y.Marriage,
                           Posts = y.Posts,
                           Isoverdueid = y.Isoverdueid,
                           NumberofContracts = y.NumberofContracts,
                           NumberofOverdues = y.NumberofOverdues,
                           Sex = y.Sex,
                           ResidentialAddress = y.ResidentialAddress,
                           Workplace = y.Workplace,
                           workingHours = y.workingHours,
                           SocialID = y.SocialID,
                           FamilyMembers = b.FamilyMembers,
                           Relationship = b.Relationship,
                           MonthlyIncome = b.MonthlyIncome,
                           Children = b.Children,
                           Urgent = b.Urgent,
                           SContact = b.SContact,
                           Sidentitycard = b.Sidentitycard,
                           JobTitle = b.JobTitle,
                           Guarantee = b.Guarantee,
                           OtherIncome = b.OtherIncome,
                           TypeOfHouse = b.TypeOfHouse,
                           Description = b.Description,

                       };
            return list.ToList();


        }

    }
}
